package com.maiji.cloud.controller;

import com.maiji.cloud.entities.shopingmall.ProvinceCity;
import com.maiji.cloud.entities.shopingmall.RuleFreight;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.shopingmall.FindByIdReqDto;
import com.maiji.cloud.request.shopingmall.ShopingAddressAddReqDto;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.shopingmall.ShopingAddressResData;
import com.maiji.cloud.service.RuleFreightService;
import com.maiji.cloud.service.ShopingAddressService;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/shippingAddress/")
public class ShopingAddressController {

    @Autowired
    private ShopingAddressService shopingAddressService;
    @Autowired
    private RuleFreightService ruleFreightService;

    /**
     *  添加新/修改收货地址
     * @param shopingAddressAddReqDto
     * @param token
     * @return
     */
    @PostMapping("addOrUpdateShopingAddress")
    public BaseDataResDto<String> addOrUpdateShopingAddress (@RequestBody ShopingAddressAddReqDto shopingAddressAddReqDto, @RequestHeader("maijiToken") String token) {
        ShopingAddressAddReqDto.ShopingAddressAddReqReqData data = shopingAddressAddReqDto.getData();
        if (data == null) return new BaseDataResDto(Status.PARAMETERERROR.setMessage("请求参数错误"));
        if (StringUtil.isBlank(data.getConsignee())) return new BaseDataResDto(Status.PARAMETERERROR.setMessage("收货人不可为空"));
        if (StringUtil.isBlank(data.getTel())) return new BaseDataResDto(Status.PARAMETERERROR.setMessage("收货电话不可为空"));
        if (! data.getTel().matches("^[1][0-9]{10}$")) return new BaseDataResDto(Status.PARAMETERERROR.setMessage("收货电话格式错误"));
        if (StringUtil.isBlank(data.getProvince())) return new BaseDataResDto(Status.PARAMETERERROR.setMessage("收货地址省/市不可为空"));
        if (StringUtil.isBlank(data.getCity())) return new BaseDataResDto(Status.PARAMETERERROR.setMessage("收货地址市/区不可为空"));
        return shopingAddressService.addOrUpdateShopingAddress(data, token);
    }

    /**
     *  获取收货地址列表
     * @param token
     * @return
     */
    @PostMapping("findAllShopingAddress")
    public BaseMetaResDto<List<ShopingAddressResData>> findAllShopingAddress (@RequestHeader("maijiToken") String token) {
        return shopingAddressService.findAllShopingAddress(token);
    }

    /**
     *  删除收货地址
     * @param findByIdReqDto
     * @return
     */
    @PostMapping("delShopingAddress")
    public BaseResDto delShopingAddress (@RequestBody FindByIdReqDto findByIdReqDto) {
        String shopingAddressId = findByIdReqDto.getData();
        if (StringUtil.isBlank(shopingAddressId)) return new BaseResDto(Status.ERROR);
        return shopingAddressService.delShopingAddress(shopingAddressId);
    }

    /**
     *  获取运费规则列表123
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllRuleFreights")
    public BaseMetaResDto<List<RuleFreight>> findAllRuleFreights (@RequestBody BaseDataReqDto<RuleFreight> baseDataReqDto) {
        RuleFreight ruleFreight = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        return ruleFreightService.findAllRuleFreights(ruleFreight, metaData);
    }

    /**
     *  添加/修改运费规则
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("add_updRuleFreight")
    public BaseResDto add_updRuleFreight (@RequestBody BaseDataReqDto<RuleFreight> baseDataReqDto) {
        RuleFreight ruleFreight = baseDataReqDto.getData();
        return ruleFreightService.add_updRuleFreight(ruleFreight);
    }

    /**
     *  获取全国省市列表
     * @return
     */
    @PostMapping("findAllProvinceCity")
    public BaseDataResDto<List<ProvinceCity>> findAllProvinceCity () {
        return ruleFreightService.findAllProvinceCity();
    }

}

